﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.Common;
using System.Data.SqlClient;
using System.Configuration;
using nSin.TaskSwitcher;

namespace nSin
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            //txtDataRiferimentoContributi.Text = "15/" + (DateTime.Now.Month - 1) + "/" + DateTime.Now.Year;
        }


        private int GetMatchingForm(string strIn)
        {

            int intHighestInstance = -1;

            foreach (TabItem obj in this.tcOpenForms.Items)
            {

                TabItemMetaData objTabItemMetaData = (TabItemMetaData)obj.Tag;

                if (objTabItemMetaData.TabHeader == strIn)
                {

                    if (objTabItemMetaData.InstanceCount > intHighestInstance)
                    {
                        intHighestInstance = objTabItemMetaData.InstanceCount;
                    }

                }

            }

            return intHighestInstance;

        }


        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            TabItemMetaData objTabItemMetaData = null;
            if (((System.Windows.Controls.MenuItem)e.Source).CommandParameter != null)
            {
                objTabItemMetaData = ((TabItemMetaData)((System.Windows.Controls.MenuItem)e.Source).CommandParameter).Clone();
                objTabItemMetaData.InstanceCount = GetMatchingForm(objTabItemMetaData.TabHeader) + 1;
            }
            else
                objTabItemMetaData = new TabItemMetaData("application name " + ((System.Windows.Controls.MenuItem)e.Source).Header, "strTabName " + ((System.Windows.Controls.MenuItem)e.Source).Header, "form" + ((System.Windows.Controls.MenuItem)e.Source).Header);
            //Assembly asm = Assembly.GetExecutingAssembly;
            //UserControl objFormControl = asm.CreateInstance(objTabItemMetaData.UserControlFormName, true);
            UserControl myControl = null;
            switch (((System.Windows.Controls.MenuItem)e.Source).Header.ToString())
            {
                case "Importa iscritti CreVal":
                    myControl = new ImportaFileIscritti("CREVAL");
                    break;
                case "Importa iscritti POPSO":
                    myControl = new ImportaFileIscritti("POPSO");
                    break;
                case "Invio mail":
                    myControl = new InvioMail();
                    break;
                case "Visualizza elenco iscritti":
                    myControl = new ComparaIscritti();
                    break;
                case "Compara elenco iscritti":
                    myControl = new SelezioneMesiComparazione();
                    break;
                case "Elenco telefonico":
                    myControl = new RicercaElencoTelefonico();
                    break;
                default:
                    MessageBox.Show("Comando non gestito");
                    break;

            }
            

            TabItem objTabItem = new TabItem();

            //if (objTabItemMetaData.InstanceCount > 0)
            //{
            //    objTabItem.Header = string.Format("{0} ({1})", objTabItemMetaData.TabHeader, objTabItemMetaData.InstanceCount.ToString());
            //}

            //else
            //{
            //    objTabItem.Header = objTabItemMetaData.TabHeader;
            //}
            objTabItem.Header = objTabItemMetaData.TabHeader;
            objTabItem.Tag = objTabItemMetaData;
            objTabItem.Content = myControl;
            this.tcOpenForms.SelectedIndex = this.tcOpenForms.Items.Add(objTabItem);
        }

        private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (this.tcOpenForms.Items.Count > 0 && this.ucTaskSwitcherControl.Visibility == System.Windows.Visibility.Collapsed && e.Key == Key.Tab && e.KeyboardDevice.Modifiers == ModifierKeys.Control)
            {
                this.ucTaskSwitcherControl.Visibility = System.Windows.Visibility.Visible;
                this.ucTaskSwitcherControl.Show(this.tcOpenForms);
                e.Handled = true;
            }

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (this.ucTaskSwitcherControl.Visibility == System.Windows.Visibility.Visible && e.KeyboardDevice.Modifiers != ModifierKeys.Control)
            {
                this.ucTaskSwitcherControl.Hide();
            }

        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {

        }

    }
}
